package com.dragon.concurrent.demo.waitnotify;

import java.util.Optional;
import java.util.stream.Stream;

public class BooleanLockTest {

    public static void main(String[] args) {
        BooleanLock booleanLock = new BooleanLock();
        Stream.of("M1", "M2", "M3")
                .forEach(s -> {
                    new Thread(() -> {
                        try {
                            booleanLock.lock();
                            Optional.of(Thread.currentThread().getName() + "  has a lock monitor")
                                    .ifPresent(System.out::println);
                            work();
                            booleanLock.unlock();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }, s).start();
                });
        booleanLock.unlock();

    }

    public static void work() {
        Optional.of(Thread.currentThread().getName() + "  is working").ifPresent(System.out::println);
        try {
            Thread.sleep(5_000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
